Polinomials


In [23]:
import numpy.polynomial.polynomial as p
p.polyadd([-8, 5, 2], [-2, 0, 0, 0, 3])
p.polymul([-8, 5, 2], [-2, 0, 0, 0, 3])


Out[23]:
array([ 16., -10.,  -4.,   0., -24.,  15.,   6.])

In [25]:
import sympy
from sympy.abc import x
polynomial = p.Polynomial([-2, 0, 0, 0, 3])
sympy.init_printing()
print(sympy.Poly(reversed(polynomial.coef), x).as_expr())


3.0*x**4 - 2.0

Set


In [ ]:
my_list = [2, 3, 10]
print(my_list[1])
my_set = {2, 3, 10}
print(my_set)

In [ ]:
range(5)
list(range(5))

In [ ]:
import numpy as np
np.arange(1)

In [ ]:
numbers = []
for i in range(11):
    numbers.append(i)
print(numbers)

In [ ]:
# List comprehension
numbers = [i for i in range(11)]
print(numbers)

In [ ]:
numbers = [x * y for x in range(5) for y in range(5)]
print(numbers)

In [ ]:
numbers = [x for x in range(50) if x >= 20 and x < 25]
print(numbers)

In [ ]:
positive_x = {x for x in range(-5, 5) if x >= 0}
print(positive_x)

In [ ]:
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 10, 3, 5, 10, 3, 3}
print(len(set2))
print(1 in set1)
print(10 not in set1)
print({1, 2}.issubset(set1))
print(set1.union(set2))
print(set1.difference(set2))
print(set2.difference(set1))
print(set1.symmetric_difference(set2))

In [ ]:
def add_one(x):
    return x + 1
add_one(2)

functions


In [ ]:
def linear_function(x):
    return 2 * x + 3

def square(x):
    return x * x

In [ ]:
def linear_function_f(f, x):
    return 2 * f(x) + 3

linear_function_f(square, 4)

In [ ]:
def compose(f, g, x):
    """
    Returns the composition of the functions f and g, applied to the arguments x
    """
    return f(g(x))

In [ ]:
print(compose(linear_function, square, 4))
print(compose(square, linear_function, 4))

In [ ]:
def comp(f, g):
    return lambda x: f(g(x))

In [ ]:
comp(linear_function, square)(4)

Function graphs


In [ ]:
import numpy as np
import matplotlib.pyplot as plt
def plot_function(f, x_min = -10, x_max = 10, n_values = 2000):
    x = np.linspace(x_min, x_max, n_values)
    y = f(x) # Broadcasting
    plt.plot(x, y)
    plt.show()

plot_function(lambda x: np.sin(x))

In [ ]:
import numpy as np
import matplotlib.pyplot as plt
def plot_function(f, x_min = -10, x_max = 10, n_values = 2000):
    x = np.linspace(x_min, x_max, n_values)
    y = np.vectorize(f)(x)
    plt.plot(x, y)
    plt.show()

import math
plot_function(lambda x: math.sin(x))
#plot_function(linear_function(2))

Circle

$x^2 + y^2 = 1$ or $ $


In [ ]:
r = [1] * 1000
phi = np.linspace(0, 2 * np.pi, 1000)
plt.polar(phi, r)
plt.show()

In [ ]:
r = [1] * 1000
phi = np.linspace(0, 2 * np.pi, 1000)

x = r * np.cos(phi)
y = r * np.sin(phi)

plt.plot(x, y)
plt.gca().set_aspect("equal") # get current axis and set its property
plt.show()

Number Fields

Complex Numbers

In [37]:
print((3 + 2j) + (1 - 1j))
print((3 + 2j) * (1 - 1j))
z = (3 + 2j) * (1 - 1j)
print([z.real, z.imag])


(4+1j)
(5-1j)
[5.0, -1.0]

Fundamental Theorem of Algebra


In [42]:
import cmath
def solve_quadratic_equation(a, b, c):
        discriminant = cmath.sqrt(b * b - 4 * a * c)
        return [
            (-b + discriminant) / (2 * a),
            (-b - discriminant) / (2 * a)
        ]
print(solve_quadratic_equation(1, -3, -4))
print(solve_quadratic_equation(1, 0, -4))
print(solve_quadratic_equation(1, 2, 1))
print(solve_quadratic_equation(1, 4, 5))


[(4+0j), (-1+0j)]
[(2+0j), (-2+0j)]
[(-1+0j), (-1+0j)]
[(-2+1j), (-2-1j)]

In [ ]: